/**
 * @author zhangLin
 * 驻守后台：守护线程 Daemon
 */
public class DaemonDemo {
    public static class DaemonT extends Thread {
        @Override
        public void run(){
            while (true) {
                System.out.println("I am alive");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t = new DaemonT();
        /**
         * 这里设置守护线程。如果不设置或在start之后设置则主线程结束后还会继续运行
         */
        t.setDaemon(true);
        t.start();

        Thread.sleep(2000);
        System.out.println("主线程结束了");
    }
}
